<template>
  <el-row class="container">
    <el-col :span="24" class="header">
      <el-col
        :span="10"
        class="logo"
        :class="collapsed?'logo-collapse-width':'logo-width'"
      >{{collapsed?'':sysName}}</el-col>
      <el-col :span="1">
        <div class="tools" @click.prevent="collapse">
          <i class="fa fa-align-justify"></i>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="user-title"></div>
      </el-col>
      <el-col :span="4" class="userinfo">
        <el-dropdown trigger="hover">
          <span class="el-dropdown-link userinfo-inner">
            <img :src="this.sysUserAvatar" />
            {{userName}}
            <!-- ，{{sysUserName}} -->
          </span>
          <el-dropdown-menu slot="dropdown">
            <!-- <el-dropdown-item @click.native="myMessage">
              我的消息
              <span class="tipNum" v-show="messageNum != 0">{{messageNum}}</span>
            </el-dropdown-item>-->
            <!-- <el-dropdown-item>设置</el-dropdown-item> -->
            <el-dropdown-item divided @click.native="changePassword">修改密码</el-dropdown-item>
            <el-dropdown-item divided @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-col>
    <el-col :span="24" class="main">
      <aside :class="collapsed?'menu-collapsed':'menu-expanded'">
        <!--导航菜单-->
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          @open="handleopen"
          @close="handleclose"
          @select="handleselect"
          unique-opened
          router
          v-show="!collapsed"
          style="width:100%"
        >
          <!-- <template v-for="(item,index) in $router.options.routes" v-if="!item.hidden"> -->
          <template v-for="(item,index) in routerArr" v-if="!item.hidden">
            <el-submenu :index="index+''" v-if="!item.leaf">
              <template slot="title">
                <i :class="item.iconCls"></i>
                {{item.name}}
              </template>
              <el-menu-item
                v-for="child in item.children"
                :index="child.path"
                :key="child.path"
                v-if="!child.hidden"
              >{{child.name}}</el-menu-item>
            </el-submenu>
            <el-menu-item v-if="item.leaf&&item.children.length>0" :index="item.children[0].path">
              <i :class="item.iconCls"></i>
              {{item.children[0].name}}
            </el-menu-item>
          </template>
        </el-menu>
        <!--导航菜单-折叠后-->
        <ul class="el-menu el-menu-vertical-demo collapsed" v-show="collapsed" ref="menuCollapsed">
          <!-- <li
            v-for="(item,index) in $router.options.routes"
            :key="index"
            v-if="!item.hidden"
            class="el-submenu item"
          >-->
          <li
            v-for="(item,index) in routerArr"
            :key="index"
            v-if="!item.hidden"
            class="el-submenu item"
          >
            <template v-if="!item.leaf">
              <div
                class="el-submenu__title"
                style="padding-left: 20px;"
                @mouseover="showMenu(index,true)"
                @mouseout="showMenu(index,false)"
              >
                <i :class="item.iconCls"></i>
              </div>
              <ul
                class="el-menu submenu"
                :class="'submenu-hook-'+index"
                @mouseover="showMenu(index,true)"
                @mouseout="showMenu(index,false)"
              >
                <li
                  v-for="child in item.children"
                  v-if="!child.hidden"
                  :key="child.path"
                  class="el-menu-item"
                  style="padding-left: 40px;"
                  :class="$route.path==child.path?'is-active':''"
                  @click="$router.push(child.path)"
                >{{child.name}}</li>
              </ul>
            </template>
            <template v-else>
              <li class="el-submenu">
                <div
                  class="el-submenu__title el-menu-item"
                  style="padding-left: 20px;height: 56px;line-height: 56px;padding: 0 20px;"
                  :class="$route.path==item.children[0].path?'is-active':''"
                  @click="$router.push(item.children[0].path)"
                >
                  <i :class="item.iconCls"></i>
                </div>
              </li>
            </template>
          </li>
        </ul>
      </aside>
      <section class="content-container">
        <div class="grid-content bg-purple-light">
          <el-col :span="24" class="breadcrumb-container">
            <strong class="title">{{$route.name}}</strong>
            <el-breadcrumb separator="/" class="breadcrumb-inner">
              <el-breadcrumb-item v-for="item in $route.matched" :key="item.path">{{ item.name }}</el-breadcrumb-item>
            </el-breadcrumb>
          </el-col>
          <el-col :span="24" class="content-wrapper">
            <transition name="fade" mode="out-in">
              <router-view></router-view>
            </transition>
          </el-col>
        </div>
      </section>
    </el-col>
    <!-- <div class="my-message" @click="myMessage">消息:{{messageNum}}</div> -->
    <el-col :span="24" class="my-message">
      <el-badge :value="messageNum" class="item">
        <el-button size="small" @click="myMessage">消息</el-button>
      </el-badge>
    </el-col>
    <audio v-show="false" hidden="true" id="audio" muted="muted" src="../../static/text2audio.mp3" />
    <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%">
      <el-input v-model="password" placeholder="请输入密码"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureChangePassword">确 定</el-button>
      </span>
    </el-dialog>
  </el-row>
</template>

<script>
import { logout, userChangeMePassword } from "../api/baseSetting";
import {
  wlcSysInquiryListNotice,
  wlcNoticeList,
  wlcPurchaseOrderLogListNotice,
} from "../api/api";
import moment from "moment";
export default {
  data() {
    return {
      sysName: "万绿城集团采购系统1.0版",
      // sysName:'',
      collapsed: false,
      dialogVisible: false, //修改密码弹窗是否显示
      password: "",
      sysUserName: "",
      sysUserAvatar: require("../assets/userImg.png"),
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      routerArr: [],
      audioPlay: "",
      userName: "",
      isClosePage: false, //是否关闭页面
    };
  },
  beforeDestroy() {
    this.isClosePage = true;
  },
  methods: {
    //我的消息
    myMessage() {
      this.$router.push({
        path: "/myMessage",
      });
    },
    onSubmit() {
      console.log("submit!");
    },
    handleopen() {
      //console.log('handleopen');
    },
    handleclose() {
      //console.log('handleclose');
    },
    handleselect: function (a, b) {},
    //退出登录
    logout: function () {
      var _this = this;
      this.$confirm("确认退出吗?", "提示", {
        //type: 'warning'
      })
        .then(() => {
          logout({}).then((res) => {
            this.$store.commit("setToken", "");
            sessionStorage.removeItem("token");
            localStorage.removeItem("token");
            _this.$router.push("/login");
          });
        })
        .catch(() => {});
    },
    //点击修改密码
    changePassword() {
      this.dialogVisible = true;
    },
    //确认修改密码
    sureChangePassword() {
      if (this.password) {
        userChangeMePassword({
          password: this.password,
        }).then((res) => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogVisible = false;
        });
      } else {
        this.$message({
          message: "请输入密码",
          type: "warning",
        });
      }
    },
    //折叠导航栏
    collapse: function () {
      this.collapsed = !this.collapsed;
    },
    showMenu(i, status) {
      this.$refs.menuCollapsed.getElementsByClassName(
        "submenu-hook-" + i
      )[0].style.display = status ? "block" : "none";
    },
    //获取未处理的数量
    getMessage() {
      let para = {
        page: { page: 1, pageSize: 1000 },
      };
      wlcSysInquiryListNotice(para).then((res) => {
        let s1 = res ? res.total : 0;
        let arr = res.records || [];
        let isPlay = false;
        for (let i = 0; i < arr.length; i++) {
          let expiredTime = moment(arr[i].modifytime).add(1, "m").unix();
          let nowTime = moment().unix();
          if (nowTime < expiredTime) {
            isPlay = true;
            break;
          }
        }
        wlcNoticeList(para).then((res) => {
          s1 = s1 + (res.total || 0);
          let list = res.records || [];
          for (let i = 0; i < list.length; i++) {
            let expiredTime = moment(list[i].CREATETIME).add(1, "m").unix();
            let nowTime = moment().unix();
            if (nowTime < expiredTime) {
              isPlay = true;
              break;
            }
          }
          wlcPurchaseOrderLogListNotice(para).then((res) => {
            s1 = s1 + (res.total || 0);
            this.$store.commit("setMessageNum", s1);
            if (isPlay) {
              this.aplayAudio();
            }
            if (!this.isClosePage) {
              setTimeout(() => {
                if (!this.isClosePage) {
                  this.getMessage();
                }
              }, 20000);
            }
          });
        });
      });
    },
    //语音提示
    aplayAudio() {
      const audio = document.getElementById("audio");
      let that = this;
      this.$notify({
        title: "未处理消息",
        message: "您有未处理的消息请尽快处理,可点击查看",
        position: "bottom-right",
        onClick: () => {
          that.$router.push({
            path: "/myMessage",
          });
        },
      });
      audio.play();
    },
  },
  computed: {
    messageNum() {
      return this.$store.state.messageNum;
    },
  },
  mounted() {
    this.getMessage();
    // this.aplayAudio();
    var menus = JSON.parse(sessionStorage.getItem("menus"));
    this.userName = sessionStorage.getItem("userName");
    var allMenus = this.$router.options.routes;
    var routerArr = [];
    // this.routerArr = allMenus;

    //配置的路由
    for (var b = 0; b < allMenus.length; b++) {
      var obj = {
        path: allMenus[b].path,
        name: allMenus[b].name,
        iconCls: allMenus[b].iconCls,
        children: [],
        leaf: allMenus[b].leaf,
        component: allMenus[b].component,
      };
      if (allMenus[b].children && allMenus[b].children.length > 0) {
        var childArr = allMenus[b].children;
        // console.log(childArr);
        for (var c = 0; c < childArr.length; c++) {
          //配置的路由0
          for (var a = 0; a < menus.length; a++) {
            if (childArr[c].path == menus[a].url) {
              obj.children.push(childArr[c]);
            }
          }
        }
      }
      if (obj.children.length > 0) {
        routerArr.push(obj);
      }
    }
    this.routerArr = routerArr;
  },
};
</script>

<style scoped lang="scss">
@import "~scss_vars";
.container {
  position: absolute;
  top: 0px;
  bottom: 1px;
  width: 100%;
  .header {
    height: 60px;
    line-height: 60px;
    background: $color-primary;
    color: #fff;
    .userinfo {
      text-align: right;
      padding-right: 35px;
      float: right;
      .userinfo-inner {
        cursor: pointer;
        color: #fff;
        img {
          width: 40px;
          height: 40px;
          border-radius: 20px;
          margin: 10px 0px 10px 10px;
          float: right;
        }
      }
    }
    .logo {
      //width:230px;
      height: 60px;
      font-size: 16px;
      padding-left: 20px;
      padding-right: 20px;
      border-color: rgba(238, 241, 146, 0.3);
      border-right-width: 1px;
      border-right-style: solid;
      img {
        width: 40px;
        float: left;
        margin: 10px 10px 10px 18px;
      }
      .txt {
        color: #fff;
      }
    }
    .logo-width {
      width: 230px;
    }
    .logo-collapse-width {
      width: 60px;
    }
    .tools {
      padding: 0px 23px;
      width: 14px;
      height: 60px;
      line-height: 60px;
      cursor: pointer;
    }
    .user-title {
      display: flex;
      justify-content: space-around;
    }
  }
  .main {
    display: flex;
    position: absolute;
    top: 60px;
    // bottom: 0px;
    bottom: 60px;
    overflow: hidden;
    aside {
      flex: 0 0 230px;
      width: 230px;
      // position: absolute;
      // top: 0px;
      // bottom: 0px;
      .el-menu {
        height: 100%;
      }
      .collapsed {
        width: 60px;
        .item {
          position: relative;
        }
        .submenu {
          position: absolute;
          top: 0px;
          left: 60px;
          z-index: 99999;
          height: auto;
          display: none;
        }
      }
    }
    .menu-collapsed {
      flex: 0 0 60px;
      width: 60px;
    }
    .menu-expanded {
      flex: 0 0 230px;
      width: 230px;
    }
    .content-container {
      // background: #f1f2f7;
      flex: 1;
      // position: absolute;
      // right: 0px;
      // top: 0px;
      // bottom: 0px;
      // left: 230px;
      overflow-y: scroll;
      padding: 20px;
      .breadcrumb-container {
        //margin-bottom: 15px;
        .title {
          width: 200px;
          float: left;
          color: #475669;
        }
        .breadcrumb-inner {
          float: right;
        }
      }
      .content-wrapper {
        background-color: #fff;
        box-sizing: border-box;
      }
    }
  }
  .my-message {
    position: absolute;
    bottom: 0px;
    text-align: right;
    height: 60px;
    background: #f5f5f5;
    line-height: 60px;
    z-index: 30;
    box-sizing: border-box;
    padding-right: 30px;
    // cursor: pointer;
    // font-size: 16px;
    // padding: 5px 10px;
    // border:1px solid #e5e5e5;
    // border-radius: 5px;
    // &:hover{
    //   // #F5F5F5
    //   background: #F5F5F5;
    // }
  }
}
.el-dropdown-menu__item {
  .tipNum {
    width: 15px;
    height: 15px;
    background: red;
    color: #fff;
    border-radius: 15px;
    font-size: 12px;
    text-align: center;
    line-height: 15px;
    float: right;
    margin: 10px 0 0 5px;
  }
}
</style>
<style lang="less">
.container {
  .my-message {
    .el-badge__content.is-fixed {
      top: 17px;
    }
  }
}
</style>